package com.apexedu.biz.action;

import com.apexedu.biz.Const;
import com.apexedu.biz.entity.TAccessAppReg;
import com.apexedu.biz.service.impl.PortalAuthorizationServiceImpl;
import com.apexedu.framework.service.BaseService;
import com.apexedu.identity.entity.TSysMenu;
import com.apexedu.identity.entity.TSysRole;
import com.apexedu.util.StringUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 门户授权
 * User: hc
 * Date: 14-5-15
 * Time: 下午2:33
 */
@SuppressWarnings("serial")
@Component
@Scope("prototype")
public class PortalAuthorizationAction extends BizBaseAction {
    @Resource(name = "portalAuthorizationService")
    public void setBaseService(BaseService baseService) {
        super.setBaseService(baseService);
    }

    private List<TSysRole> roleList;//角色信息

    private List<TAccessAppReg> listApp; //业务系统列表

    private String msg;//记录保存是否成功

    private int appno;//成功后刷新页面

    private TSysRole tsysRole;//角色，查询时使用

    public TSysRole getTsysRole() {
        return tsysRole;
    }

    public void setTsysRole(TSysRole tsysRole) {
        this.tsysRole = tsysRole;
    }

    public int getAppno() {
        return appno;
    }

    public void setAppno(int appno) {
        this.appno = appno;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    public List<TSysRole> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<TSysRole> roleList) {
        this.roleList = roleList;
    }

    public List<TAccessAppReg> getListApp() {
        return listApp;
    }

    public void setListApp(List<TAccessAppReg> listApp) {
        this.listApp = listApp;
    }


    //返回角色名称
    public String queryAllRole() {
        PortalAuthorizationServiceImpl pas = (PortalAuthorizationServiceImpl) this.getBaseService();
        String appid = getRequest().getParameter("appid");
        //用于绑定页面刷新后显示
        String appno = getRequest().getParameter("appno");
        List<TSysRole> rolelist = pas.queryAllRole(appid, appno);
        //得到所有角色id，拼接成字符串
        StringBuffer rolebuffer = new StringBuffer();
        for (int i = 0; i < rolelist.size(); i++) {
            rolebuffer.append(rolelist.get(i).getRoleid());
            if (i < rolelist.size() - 1) {
                rolebuffer.append(",");
            }
        }
        List<TSysMenu> menulist = pas.queryPortalMenu();
        List<TAccessAppReg> appRegs = pas.queryAllMenu();
        //默认第一次显示的接入系统为集合第一个
        if (StringUtil.isBlank(appid)) {
            appid = appRegs.get(0).getAppid();
        }
        this.setParameter("rolebuffer", rolebuffer);
        this.setParameter("appid", appid);
        this.setParameter("rolelist", rolelist);
        this.setParameter("menulist", menulist);
        this.setParameter("taccessAppRegid", Const.PortalMenu);
        this.setParameter("portalname","门户资源");
        return toView("rolemenu.jsp");
    }
    //点击角色保存资源
    public String saveRoleMenu(){
        PortalAuthorizationServiceImpl pas = (PortalAuthorizationServiceImpl) this.getBaseService();
        String roleid = getRequest().getParameter("roleid");
        String menus = getRequest().getParameter("menus");
        String msg = pas.saveRoleMenu(roleid,menus);
        try {
            pas.load(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setParameter("msg",msg);
        return toView("input.jsp");
    }
}
